package com.xthh._function.js;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MyJSON {
    public static int getInt(JSONObject object, String code) {
        if (object == null || code == null || code.length() <= 0)
            return 0;

        if (object.isNull(code))
            return 0;

        try {
            return object.getInt(code);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return 0;
    }


    public static String getString(JSONObject object, String code) {
        if (object == null || code == null || code.length() <= 0)
            return "";

        if (object.isNull(code))
            return "";

        try {
            return object.getString(code);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return "";
    }

    public static JSONArray getJSONArray(JSONObject object, String code) {
        if (object == null || code == null || code.length() <= 0)
            return null;

        if (object.isNull(code))
            return null;

        try {
            return object.getJSONArray(code);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }


    public static JSONObject getJSONObject(JSONObject object, String code) {
        if (object == null || code == null || code.length() <= 0)
            return null;

        if (object.isNull(code))
            return null;

        try {
            return object.getJSONObject(code);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }
}
